/**
 *  *2010-10-16 上午12:34:36  add 
 */
package com.base.myproject.client.busi.contract;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.ccode.CcodeSelect;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseCheckGroup;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 电话销售，记录销售的整个过程
 * 
 * @author Administrator 2010-10-16 javazgw@gmail.com
 */
public class TelSale extends BaseJDBCPanel {
	ModelType type;
	CcodeSelect ccodeselect = new CcodeSelect(1);
	BaseComboBoxX C_num5 = new BaseComboBoxX("lname","ccodelinkman_info", "where 1=2");
	BaseComboBoxForm<BaseModelData> C_num4;
	TextArea C_num7 ;
	public TelSale() {
		super();
		init();
	}

	public void complete() {
		super.complete();
		StoreFilterField<BusiObject> field = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
				for (int i = 0; i < type.getFieldCount(); i++) {

					Object name = record.get(type.getField(i).getName());

					// TODO
					if (name != null
							&& name.toString().toLowerCase().indexOf(
									filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}

			@Override
			protected void onFilter() {
				super.onFilter();

				// grid.getSelectionModel().select(0);
			}

		};
		// ttiratify.setText("提交确认");
		/*field.setAutoWidth(true);
		field.setToolTip("对当前页面进行查找");
		field.bind(store);
		field.setFieldLabel("当页搜索");
		*/
		Button b = new Button("test");
		b.addSelectionListener(new SelectionListener<ButtonEvent>()
				{

					@Override
					public void componentSelected(ButtonEvent ce) {
						 El disablemask = new El(
						"<div class=\"base-ext-el-mask\" style=\"display: block;\"></div>");
						el().appendChild(disablemask.dom);

						addStyleName("baseformdisable");
						
					}});
		getToolbarll().add(b);
		/*getToolbarll().add(field);*/
	}

	public void init() {
		this.setWidth("920px");
		this.setAutoHeight(true);
		this.add(initWeigetx());

		registerMainStore(getStore());
		registerStore(getStore());
		setdefault();
		//C_num4.addListener(Events.Changed, getListener());
		//C_num5.addListener(Events.Changed, getListener());
	}

	public Widget initWeigetx() {
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(createGrid());
		
		vpanel.add(createForm()); 
		//HorizontalPanel hpanel = new HorizontalPanel();
		return vpanel;
	}

	private void setdefault() {

		store.addDefault("bcode", Env.getEnvbcode());

		store.addDefault("teldate", new Date());

	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("teldate");
		type.getField("teldate").setType(Date.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("ccodelinkman");
		type.getField("ccodelinkman").setType(String.class);
		type.addField("status");
		type.getField("status").setType(Integer.class);
		type.addField("remark");
		type.getField("remark").setType(String.class);
		type.addField("telgoal");
		type.getField("telgoal").setType(Integer.class);
		type.addField("codename");
		StoreInfo info = new StoreInfo();
		info.setTablename("bu_telsale");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from bu_telsale order by teldate desc");
		
			info.addBusiParams("CODENAMEPARAMS", "{D:MMdd}{-}{INC:4}");
			info.addBusiParams("CODENAMEFILTER", "1=1");
			info.addBusiParams("CODENAME", "codename");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		

		store = new BaseStore<BusiObject>(type, info);
//		store.addEditdisableItem(new Editdisable("icode","27",EditDisableType.IN));
//		store.addEditdisableItem(new Editdisable("icode","26",EditDisableType.IN));
//		store.addEditdisableItem(new Editdisable("icode","28",EditDisableType.IN));
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C0 = new ColumnConfig("codename", "编号", 80);
		columns.add(C0);
		 ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		 columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("bcode", "部门人员", 80);
		C2.setEditor(new CellEditor(new TextField()));
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("teldate", "时间", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("ccode", "客户", 80);
		CcodeSelect ccodeselect2 = new CcodeSelect(1);
		C4.setRenderer(ccodeselect2.getCcodeSelect());
		C4.setEditor(ccodeselect2.getCcodeSelect().getCelEditor());
		columns.add(C4);
		
		ColumnConfig C5 = new ColumnConfig("ccodelinkman", "联系人", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("status", "状态", 80);
		columns.add(C6);
		// ColumnConfig C7 = new ColumnConfig("remark", "remark", 80);
		// columns.add(C7);
		// ColumnConfig C8 = new ColumnConfig("telgoal", "telgoal", 80);
		// columns.add(C8);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(350);
		return grid;
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		
		
		final BaseFormPanel form = new BaseFormPanel(getStore());
		
		TableLayout tl = new TableLayout(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		TableData td2 = new TableData();
		td2.setRowspan(1);
		td2.setColspan(3);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);

		
		form.setLayout(tl);
		form.setHeaderVisible(false);

	
		form.add(new Label("部门人员:"), td);
		TextField<String> C_num2 = new TextField<String>();
		C_num2.setName("bcode");
		form.add(C_num2, td);
		form.add(new Label("电话日期:"), td);
		DateField C_data3 = new DateField();
		C_data3.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd hh:mm:ss"));
		C_data3.setName("teldate");
		form.add(C_data3, td);
		form.add(new Label("客户:"), td);
		
		 C_num4 = ccodeselect.getCcodeSelect();
		C_num4.setName("ccode");
		form.add(C_num4, td);
		form.add(new Label("联系人:"), td);
		
		C_num5.setName("ccodelinkman");
		form.add(C_num5, td);
		
		
		form.add(new Label("备注:"), td);
		 C_num7 = new TextArea();
		
		C_num7.setName("remark");
		form.add(C_num7, td2);
		
		
		form.add(new Label("目标:"), td);
		BaseCheckGroup C_num8 = new BaseCheckGroup(new String[]{"新客户挖掘","推销产品","打听其目前状况","过节祝贺","送礼"});
		
		//C_num8.setPropertyEditorType(Integer.class);
		C_num8.setName("telgoal");
		form.add(C_num8, td);
		form.add(new Label("状态:"), td);
		BaseCheckGroup C_num6 = new BaseCheckGroup(new String[]{"恼怒","无意向","小小感兴趣","有提问，需要发资料","强烈的购买欲望","需要继续回访"});
		
		C_num6.setName("status");
		form.add(C_num6, td);
		

		
		
		
		form.inited(149);
		C_num7.setWidth(313);
		C_num7.setHeight(80);
		
		
		//form.setIsdisablebystore(true);

		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals("")) {

							}
						} else if (be.getType() == Events.Changed) {
							if (be.getSource().equals(C_num4)) {
								if(C_num4.getValueCode()!=null)
									C_num5.setFilter("  where ccode='"+C_num4.getValueCode()+"'");
							}
							else if (be.getSource().equals(C_num5)) {
								if(C_num5.getValue()!=null)
									setMarkValue(store.getCurcowindex(),C_num4.getValueCode(),C_num5.getValue().get("code").toString());
							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	void setMarkValue(int index,String ccode,String lname)
	{
		String sql = "select dept,headship,telephone,mobil,fax,addr from ccodelinkman_info where ccode = '"+ccode+"' and lname = '"+lname+"'";
		
		 final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);
	greetingService.getDataSet(sql, new AsyncCallback<DataSet>(){

		@Override
		public void onFailure(Throwable caught) {
			
			
		}

		@Override
		public void onSuccess(DataSet result) {
			String str = "{";
			
			for(int i =0 ;i<result.getRowCount();i++)
			{
				str+="部门："+result.getValue(i, "dept");
				str+="职位："+result.getValue(i, "headship");
				str+="电话："+result.getValue(i, "telephone");
				str+="手机："+result.getValue(i, "mobil");
				str+="传真："+result.getValue(i, "fax");
				str+="地址："+result.getValue(i, "addr");
			}
			str+="}";
			
			String mark = C_num7.getValue();
		
			if(mark!=null && mark.indexOf("{")>=0)
			{
				C_num7.setValue(mark.replaceAll( "\\{.*\\}",str));
			}
			else
			{
				C_num7.setValue(str +(mark==null?"":mark));
			}
		}
		
	});
		
		
	}
}
